package cn.helele.controller;

import cn.helele.entity.User;
import cn.helele.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User getUserById(@PathVariable int id) {
        User user = userService.getUserById(id);
        log.info(user.toString());
//        try {
//            // 为了演示超时现象,这里使用线程休眠,时间随机,休眠0~2000毫秒
//            Thread.sleep(new Random().nextInt(2000));
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return user;
    }

    @GetMapping("/all")
    public List<User> getUserAll() {
        return userService.getUserAll();
    }
}
